import React, { Component } from 'react'
import './index.css'

export default class Item extends Component {

  state = {mouse: false}

  //鼠标移入、移出事件
  handleMouse = (flag) => {
    return () => {
      this.setState({mouse: flag})
    }
  }

  //更新checked
  changeCheckbox = (index) => {
   return (e) => {
    this.props.changeTodo(index, e.target.checked)
   }
  }

  handleDel = (id) => {
    return (e) => {
      this.props.delTodo(id)
    }
  }

  render() {
    const {id, name, done, index} = this.props
    const {mouse} = this.state
    return (
      // defaultChecked={done}
      <li className="item-list" onMouseLeave={this.handleMouse(false)} onMouseEnter={this.handleMouse(true)}>
        <label>
          <input type="checkbox" checked={done}  onChange={this.changeCheckbox(index)}/>
          <span>{name}</span>
        </label>
        <button className="btn btn-danger" onClick={this.handleDel(id)} style={{display: mouse ? 'inline-block' : 'none'}}>删除</button>
      </li>
    )
  }
}
